package com.kedou.sdk.ui.content;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.core.app.JobIntentService;

import com.kedou.sdk.assist.Constant;
import com.kedou.sdk.assist.Constant.LocalIntentExtra;
import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.utils.WL;

public class TimeTaskService extends JobIntentService implements Handler.Callback {

    private static final String TAG = TimeTaskService.class.getSimpleName();

    private static final int JOB_ID = 10000;
    private static final long TIMED_TASK_SLEEP_MILLIS_UNIT = 1000L;
    private static final long TIMED_TASK_SLEEP_MILLIS = 60 * TIMED_TASK_SLEEP_MILLIS_UNIT;

    private Handler mTimedTaskHandler;


    public static void enqueueWork(Context context) {

                    Intent work = new Intent();
//                    work.putExtra(LocalIntentExtra.INTENT_KEY_LOGIN_BINDING_DATA, config);
                    enqueueWork(context, TimeTaskService.class, JOB_ID, work);



    }

    @Override
    public void onCreate() {
        super.onCreate();
        WL.d(TAG, "onCreate ===========>");
        mTimedTaskHandler = new Handler(this);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        WL.d(TAG, "onHandleWork ===========>");

        if (!isStopService()) {

            int miao = HttpEngineApi.getInstance().getEngineConduits().getPlatformConfigInfo().getMobile_countdown();

//            if (popupConfig != null) {
                serviceThreadSleep(
                        miao * TIMED_TASK_SLEEP_MILLIS_UNIT
                        );

                if (!isStopService()) {
                    mTimedTaskHandler.sendMessage(mTimedTaskHandler.obtainMessage(0, null));
//                }
            }
        }

        WL.d(TAG, "onHanleWork ===========>sleep finish.");
    }

    private boolean isStopService() {
//        //如果已经进行过身份证绑定实名认证，就停止服务
//        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
//        if (conduits != null && conduits.isLoggedIn()) {
//            RealNameInfo realNameInfo = conduits.getRealnameInfo();
//            if (realNameInfo != null) {
//                return realNameInfo.isRealNameCertified();
//            }
//        }
//
//        return true;
        return false;
    }

    private void serviceThreadSleep(long millis) {
        WL.d(TAG, "serviceThreadsleep ===========>sleep millis: " + millis);
        final long enterMethodTimeMillis = System.currentTimeMillis();
        try {
            long currentTimeMillis = System.currentTimeMillis();
            while (Math.abs(currentTimeMillis - enterMethodTimeMillis) < millis && !isStopService()) {
                Thread.sleep(TIMED_TASK_SLEEP_MILLIS_UNIT);
                currentTimeMillis = System.currentTimeMillis();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            WL.d(TAG, "serviceThreadsleep ===========>InterruptedException.");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        WL.d(TAG, "onDestroy ===========>");
    }

    @Override
    public boolean handleMessage(Message msg) {
        WL.d(TAG, "handleMessage ===========>");
//        PopupConfig popupConfig = (PopupConfig) msg.obj;
//        if(popupConfig.isIdcardPopup() || popupConfig.isPhonePopup()){
//            Intent broadcast = new Intent(Constant.LocalIntentAction.INTENT_AACTION_FORCE_REALNAME_CERTIFIES);
//            broadcast.putExtra("isIdcardPopupCloseEnable", true);
//            LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcast);

            Intent newIntent = new Intent(Constant.LocalIntentAction.INTENT_AACTION_START_LOGIN_ACTIVITY);
            newIntent.setPackage(getApplicationContext().getPackageName());
            newIntent.putExtra(LocalIntentExtra.INTENT_KEY_LOGIN_BINDING_DATA, LocalIntentExtra.INTENT_VALUE_LOGIN_INIT_BINDING);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplicationContext().startActivity(newIntent);
//        }
        return true;
    }

}
